package com.clothing.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.clothing.dao.ArticleDao;
import com.clothing.entity.Article;
import com.clothing.entity.Article.Status;
import com.clothing.service.ArticleService;
import com.clothing.utils.page.Filter;
import com.clothing.utils.page.Filter.Operator;
import com.clothing.utils.page.Order;
import com.clothing.utils.page.Order.Direction;
import com.clothing.utils.page.Page;
import com.clothing.utils.page.Pageable;

import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;

/**
 * @ClassName: ArticleServiceImpl 
 * @Description: TODO(Service) 
 * @author Charles.Tao 
 * @date 2017年9月6日 下午3:43:34 
 * @version 1.0
 */
@Service
public class ArticleServiceImpl extends BaseServiceImpl<Article, Long> implements ArticleService
{
    /** 查看点击数时间 */
    private long viewHitsTime = System.currentTimeMillis();
    @Autowired
    private CacheManager cacheManager;
    @Autowired
    private ArticleDao articleDao;
    
    @Override
    public Page<Article> findUserPage(Long userId, Status status, Pageable pageable)
    {
        List<Filter> filters = new ArrayList<Filter>();
        filters.add(new Filter("creator.id", Operator.eq, userId));
        
        if (null != status)
        {
            filters.add(new Filter("status", Operator.eq, status));
        }

        filters.add(Filter.eq("deleted", Boolean.FALSE));
        pageable.setFilters(filters);
        List<Order> orders = new ArrayList<Order>();
        orders.add(new Order("createDate", Direction.desc));
        pageable.setOrders(orders);
        
        return findPage(pageable);
    }

    @Override
    public Page<Article> findPageByTypeAndStatus(Long articleTypeId, Status status, Boolean deleted, Pageable pageable)
    {
        List<Filter> filters = new ArrayList<>();
        if (null != articleTypeId)
        {
            filters.add(new Filter("articleType.id", Operator.eq, articleTypeId));
        }

        if (null != status)
        {
            filters.add(new Filter("status", Operator.eq, status));
        }

        if (null != deleted)
        {
            filters.add(Filter.eq("deleted", deleted));
        }

        pageable.setFilters(filters);

        return findPage(pageable);
    }
    
    @Override
    public void browseArticle(Long id)
    {
        Ehcache cache = cacheManager.getEhcache(Article.BROWSE_CACHE_NAME);
        Element element = cache.get(id);
        Integer browseQuantity;
        if (element != null)
        {
            browseQuantity = (Integer) element.getObjectValue();
        }
        else
        {
            Article article = articleDao.find(id);
            if (article == null)
            {
                return;
            }
            browseQuantity = article.getBrowseQuantity();
        }
        browseQuantity++;
        cache.put(new Element(id, browseQuantity));
        long time = System.currentTimeMillis();
        if (time > viewHitsTime + Article.BROWSE_CACHE_INTERVAL)
        {
            viewHitsTime = time;
            updateBrowseQuantity();
            cache.removeAll();
        }
    }
    
    /**
     * @Title: updateBrowseQuantity 
     * @Description: TODO(修改浏览量) 
     * @param  设定文件 
     * @return void 返回类型  
     * @throws
     */
    private void updateBrowseQuantity() {
        Ehcache cache = cacheManager.getEhcache(Article.BROWSE_CACHE_NAME);
        List<?> ids = cache.getKeys();
        for (Object id : ids) {
            Article article = articleDao.find(Long.valueOf(String.valueOf(id)));
            if (article != null) {
                Element element = cache.get(id);
                Integer browseQuantity = (Integer) element.getObjectValue();
                article.setBrowseQuantity(browseQuantity);
                articleDao.merge(article);
            }
        }
    }
}
